#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.path.append('..')
from kubernetes import client, config
from conf.yaml_tool import yaml_load, yaml_dump


class NodeClient(object):
    def __init__(self):
        from log.logger import logger
        self.logger = logger
        self.ctx = client.CoreV1Api()

    def label(self, node, key, value):
        body = {
            "metadata": {
                "labels": {
                    key: value,
                }
            }
        }
        try:
            resp = self.ctx.patch_node(node, body)
            # print("Node %s has been labeled, labels = %s" % (node, resp.metadata.labels))
            self.logger.info("Node %s has been labeled, labels = %s" % (node, resp.metadata.labels))
        except Exception as e:
            self.logger.critical(e)
            return False
        return True
